package org.qiuqiu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONObject;
import org.qiuqiu.dao.ContactDao;
import org.qiuqiu.dao.GroupDao;
import org.qiuqiu.pojo.Contact;
import org.qiuqiu.pojo.User;
import org.qiuqiu.tools.NameTools;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 杜秋野
 *
 */
public class ContactAction extends ActionSupport implements	ServletResponseAware, ServletRequestAware {
	HttpServletRequest request;
	HttpServletResponse response;
	private int groupId;
	private int contactId;
	private int order;
	private String keyWord;
	private String list;
	private User user;
	private String temp;
	private Contact contact;
	

	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public int getContactId() {
		return contactId;
	}

	public void setContactId(int contactId) {
		this.contactId = contactId;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public String getList() {
		return list;
	}

	public void setList(String list) {
		this.list = list;
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public String getTemp() {
		return temp;
	}

	public void setTemp(String temp) {
		this.temp = temp;
	}
	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	/**此方法从session中获取登录用户的ID,根据此ID获得指定用户的所有联系人的信息,
	 * 并根据order的值确定联系人的顺序,按拼音排序,0为正序,1为倒序
	 * @throws IOException
	 * @throws SQLException
	 */
	public void getContactByUserId() throws IOException, SQLException{
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		ContactDao cd = new ContactDao();
		List<HashMap> contacts = new ArrayList<HashMap>();
		contacts = cd.getList(user.getUserId(), 0, 0,order);
		String result="";
		for(HashMap hm : contacts){
			JSONObject jsonObject = new JSONObject(hm);
			result += jsonObject.toString();
			result += ";";
		}
		if(result.length()!=0){
			result = result.substring(0, result.length()-1);
		}
		out.print(result);
	}
	
	/**此方法根据群组的ID获得指定群组的所有联系人的信息,
	 * 并根据order的值确定联系人的顺序,按拼音排序,0为正序,1为倒序
	 * @throws IOException
	 * @throws SQLException
	 */
	public void getContactByGroupId() throws IOException, SQLException{
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		ContactDao cd = new ContactDao();
		List<HashMap> contacts = new ArrayList<HashMap>();
		contacts = cd.getList(0, groupId, 0,order);
		Iterator<HashMap> it = contacts.iterator();
		String result="";
		for(HashMap hm : contacts){
			JSONObject jsonObject = new JSONObject(hm);
			result += jsonObject.toString();
			result += ";";
		}
		if(result.length()!=0){
			result = result.substring(0, result.length()-1);
		}
		out.print(result);
	}
	
	/**此方法根据根据指定的联系人的信息获得单个联系人的详细信息
	 * @throws IOException
	 * @throws SQLException
	 */
	public void getContactByContactId() throws IOException, SQLException{
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		ContactDao cd = new ContactDao();
		List<HashMap> contacts = new ArrayList<HashMap>();
		contacts = cd.getList(0, 0, contactId,0);
		Iterator<HashMap> it = contacts.iterator();
		String result="";
		for(HashMap hm : contacts){
			JSONObject jsonObject = new JSONObject(hm);
			result += jsonObject.toString();
			result += ";";
		}
		if(result.length()!=0){
			result = result.substring(0, result.length()-1);
		}
		out.print(result);
	}
	
	/**该方法通过传入一个联系人ID的List集合进行联系人的批量删除
	 * @throws IOException
	 */
	public void delContactByContactId() throws IOException{
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String[] arr = list.split(",");
		List<Integer> delList = new ArrayList<Integer>();
		for(String str :  arr){
			delList.add(Integer.parseInt(str));
		}
		ContactDao cd = new ContactDao();
		try {
			cd.delContact(delList);
			out.print("true");
		} catch (SQLException e) {
			e.printStackTrace();
			out.print("false");
		}
		
	}
	
	/**该方法将获得登录用户的所有群组的信息,包括每个群组包含的人数
	 * @return 跳转到forwardTo.jsp页面
	 * @throws SQLException
	 */
	public String forwardTo() throws SQLException{
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		GroupDao gd = new GroupDao();
		List<HashMap> groups = gd.getGroupInfo(user.getUserId(),0);
		session.setAttribute("groups", groups);
		return SUCCESS;
	}
	
	/**该方法根据包含联系人ID的List集合进行联系人群组的批量转移
	 * @throws IOException
	 * @throws NumberFormatException
	 * @throws SQLException
	 */
	public void moveToGroup() throws IOException, NumberFormatException, SQLException{
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String[] arr = list.split(",");
		List<Integer> forwardList = new ArrayList<Integer>();
		for(String str :  arr){
			forwardList.add(Integer.parseInt(str));
		}
		ContactDao cd = new ContactDao();
		boolean result = cd.moveContact(forwardList, Integer.parseInt(temp));
		if (result) {
			out.print("true");
		}else{
			out.print("false");
		}
	}
	
	/**通过关键字搜索联系人
	 * @throws IOException
	 * @throws SQLException
	 */
	public void getContactByKeyWord() throws IOException, SQLException{
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		keyWord = java.net.URLDecoder.decode(keyWord,"UTF-8");
		HashMap<String,String> para = NameTools.splitInput(keyWord);
		para.put("userId", new Integer(user.getUserId()).toString());
		ContactDao cd = new ContactDao();
		List<HashMap> contacts = new ArrayList<HashMap>();
		contacts = cd.instant(para);
		String result="";
		for(HashMap hm : contacts){
			JSONObject jsonObject = new JSONObject(hm);
			result += jsonObject.toString();
			result += ";";
		}
		if(result.length()!=0){
			result = result.substring(0, result.length()-1);
		}
		out.print(result);
	}
	
	/**增加联系人
	 * @throws IOException
	 * @throws SQLException
	 */
	public void addContact() throws IOException, SQLException{
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String fullName = contact.getFirstName()+contact.getSecondName();
		String pinyin = NameTools.getPinYin(fullName);
		contact.setFullName(fullName);
		contact.setPinyin(pinyin);
		ContactDao cd = new ContactDao();
		if(cd.addContact(contact)){
			out.print("true");
		}else{
			out.print("false");
		}
	}
	
	/**修改联系人信息
	 * @throws IOException
	 * @throws SQLException
	 */
	public void modContact() throws IOException, SQLException{
		response.setContentType("text/HTML;charset=UTF-8");
		PrintWriter out = response.getWriter();
		contact.setFullName(contact.getFirstName()+contact.getSecondName());
		contact.setPinyin(NameTools.getPinYin(contact.getFullName()));
		ContactDao cd = new ContactDao();
		if(cd.modContact(contact)){
			out.print(true);
		}else{
			out.print(false);
		}
	}

}
